/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tools;

import accessor.Products;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author QUYET
 */
public class MyTool {

    public MyTool() {
    }

    public String getShortDescription(String str, int noOfWord) {
        if(str==null||str.equals("")){
            return "";
        }
        String description = "";
        String[] strArr = str.split(" ");
        if (noOfWord >= strArr.length) {
            description = str;
        } else {
            for (int i = 0; i < noOfWord; i++) {
                description += strArr[i] + " ";
            }
        }

        return description;
    }

    public String createLinks(String url, int totalRows, int perPage, int offSet, int pageNo) {
        String link = "";
        int totalPage = totalRows % perPage == 0 ? totalRows / perPage : totalRows / perPage + 1;
        if (totalPage == 1) {
            return link;
        }
        for (int i = 0; i < totalPage; i++) {
            if (pageNo == i + 1) {
                link += "<b>" + (i + 1) + "</b>";
            } else {
                link += "<a href=\"" + url + "&offSet=" + (i * perPage) + "&pageNo=" + (i + 1) + "\">" + (i + 1) + "</a>";
            }

        }

        return link;
    }

    public List<Products> groupDuplicateProduct(List<Products> productCart) {
        for (int i = 0; i < productCart.size(); i++) {
            for (int j = i + 1; j < productCart.size(); j++) {
                if (productCart.get(i).getProductID() == productCart.get(j).getProductID()) {
                    productCart.get(i).setQuantity(productCart.get(i).getQuantity() + productCart.get(j).getQuantity());
                    productCart.remove(productCart.get(j));
                }
            }
        }
        return productCart;
    }
}
